//
//  ActivityRequestModel.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/8/25.
//  Copyright © 2016年 陈区. All rights reserved.
//
#import "ActivityRequestModel.h"
#import "ActivityMemberModel.h"

#define kActivityStartOrStopUrl [SEGServerManager serverUrlJoint:kClientRootURL,@"/"kUhomecp_app,@"/activity/updateActivity.json",nil]

@implementation ActivityRequestModel

// ＝＝＝＝＝＝＝＝＝ 广场 ＝＝＝＝＝＝＝＝＝
/**
 *  请求活动ID列表 可替换[[BusinessManager sharedManager].activityEntityManager requestIsEqualIdNumber];
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestIsEqualIdNumber:(SEGNetworkRequestCallBack)finished {
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/refresh-ids.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSMutableDictionary *responseDic = [NSMutableDictionary dictionaryWithDictionary:result];
        
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        
        NSArray *data = [responseDic objectForKey:@"data"];
        //数据返回正常
        if ([code isEqualToString:@"0"]) {
            //  拼参数ids
            NSMutableString *ids = [NSMutableString stringWithString:@""];
            for (int i = 0; i < data.count; i++) {
                NSString *idString = [[data[i] objectForKey:@"id"] stringValue];
                if (i == 0) {
                    [ids appendString:idString];
                }else {
                    [ids appendFormat:@",%@",idString];
                }
            }
            [responseDic setObject:ids forKey:@"ids"];
        }
        finished(responseDic,nil);
    }];
}

/**
 *  获取活动列表
 *
 *  @param ids
 */
- (void)requestActivityModelListWithIds:(NSString *)ids finished:(SEGNetworkRequestCallBack)finished
{
    NSDictionary* paramsDict = [NSDictionary dictionaryWithObjectsAndKeys:ids, @"ids", nil];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/new-list.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:paramsDict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSDictionary *responseDic = result;
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        //数据返回正常
        if ([code isEqualToString:@"0"]) {
            finished(result,nil);
        }else {
            finished(result,nil);
        }
    }];
}



// ＝＝＝＝＝＝＝＝＝ 我的 ＝＝＝＝＝＝＝＝＝

/**
 *  请求我参加的他人创建的活动列表 可替换[[BusinessManager sharedManager].activityEntityManager requestOtherCreateStartingActivityList];
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestOtherCreateStartingListActivity:(SEGNetworkRequestCallBack)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/listForPage.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSMutableDictionary *responseDic = [NSMutableDictionary dictionaryWithDictionary:result];
        
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        //数据返回正常
        if ([code isEqualToString:@"0"]) {
            finished(result,nil);
        }else {
            finished(result,nil);
        }
    }];
}

/**
 *  请求我创建的活动列表 可替换[[BusinessManager sharedManager].activityEntityManager requestMyCreateActivityListForStarting];
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestMyCreateStartingListActivity:(SEGNetworkRequestCallBack)finished
{
    // 3. 网路请求方法(必须添加)
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:2],@"status", nil];

    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/myList.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSMutableDictionary *responseDic = [NSMutableDictionary dictionaryWithDictionary:result];
        
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        //数据返回正常
        if ([code isEqualToString:@"0"]) {
            finished(result,nil);
        }else {
            finished(result,nil);
        }
    }];
}


// ＝＝＝＝＝＝＝＝＝ 发布活动 ＝＝＝＝＝＝＝＝＝

/**
 *  发起活动 可替换[[BusinessManager sharedManager].activityEntityManager requestCreateActivityWithTempActivityEntity:
 */
- (void)requestCreateActivityWithActivityModel:(ActivityModel *)model imageData:(NSData *)imageData finished:(SEGNetworkReponse)finished
{
    if(imageData)
    {
        UIImage *image = [UIImage imageWithData:imageData];
        
        [[SEGOnefileuploadManager sharedManager] requestOnefileuploadWithImage:image progress:nil completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
            if (!isSuccessed) {
                finished(isSuccessed,result,codeType,error.localizedDescription);
                return ;
            }
            [self createActivityUpdateImage:model retImageDic:result finished:finished];
        }];
        return;
    }
    
    [self createActivityUpdateImage:model retImageDic:nil finished:finished];
}


/**
 发布活动

 @param model model
 @param retImageDic dic
 @param finished 回调
 */
- (void)createActivityUpdateImage:(ActivityModel *)model retImageDic:(NSDictionary *)retImageDic finished:(SEGNetworkReponse)finished{
    // 拼接请求字典
    NSMutableDictionary *requestDic = [NSMutableDictionary dictionary];
    
    [requestDic setObjectExtra:model.name forKey:@"name"];
    [requestDic setObjectExtra:model.desc forKey:@"description"];
    [requestDic setObjectExtra:model.startDate forKey:@"startDate"];
    [requestDic setObjectExtra:model.endDate forKey:@"endDate"];
    
    [requestDic setObjectExtra:model.place forKey:@"place"];
    [requestDic setObjectExtra:model.totalPersonNum forKey:@"personNum"];
    
    NSString *groupId = [SEGUtils stringDateByFormatString:@"yyyyMMddHHmmssSSS" withDate:[NSDate date]];
    [requestDic setObjectExtra:groupId forKey:@"activityGroup"];
    
    // pic1 图片
    if(retImageDic)
    {
        [requestDic setObject:[retImageDic valueForKey:@"data"] forKey:@"pic1"];
    }
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/saveActivity.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:requestDic finished:finished];
}

/**
 *  编辑更新活动 可替换[[BusinessManager sharedManager].activityEntityManager requestUpdateActivityWithTempActivityEntity:
 */
- (void)requestUpdateActivityWithActivityModel:(ActivityModel *)model imageData:(NSData *)imageData finished:(SEGNetworkReponse)finished
{
    if(imageData)
    {
        UIImage *image = [UIImage imageWithData:imageData];
        [[SEGOnefileuploadManager sharedManager] requestOnefileuploadWithImage:image progress:nil completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
            if (!isSuccessed) {
                finished(isSuccessed, result, codeType, error.localizedDescription);
                return;
            }
            
            [self updateActivityUpdateImage:model retImageDic:result finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                finished(isSuccessed, result, codeType, errorMessage);
            }];
        }];
    }else{
        [self updateActivityUpdateImage:model retImageDic:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            finished(isSuccessed, result, codeType, errorMessage);
        }];
    }
}

-(void)updateActivityUpdateImage:(ActivityModel *)model retImageDic:(NSDictionary *)retImageDic finished:(SEGNetworkReponse)finished {
    // 拼接请求字典
    NSMutableDictionary *requestDic = [NSMutableDictionary dictionary];
    
    [requestDic setObject:model.serviceId forKey:@"serviceId"];
    [requestDic setObjectExtra:model.name forKey:@"name"];
    [requestDic setObjectExtra:model.desc forKey:@"description"];
    [requestDic setObjectExtra:model.startDate forKey:@"startDate"];
    [requestDic setObjectExtra:model.endDate forKey:@"endDate"];
    
    [requestDic setObjectExtra:model.place forKey:@"place"];
    [requestDic setObjectExtra:model.totalPersonNum forKey:@"personNum"];
    
    NSString *groupId = [SEGUtils stringDateByFormatString:@"yyyyMMddHHmmssSSS" withDate:[NSDate date]];
    [requestDic setObjectExtra:groupId forKey:@"activityGroup"];
    
    // pic1 图片
    if(retImageDic)[requestDic setObject:[retImageDic valueForKey:@"data"] forKey:@"pic1"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/update.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:requestDic finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSDictionary *responseDic = result;
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        finished(isSuccessed, result, codeType, errorMessage);
    }];
}


// ＝＝＝＝＝＝＝＝＝ 活动详情 ＝＝＝＝＝＝＝＝＝
/**
 *  获取活动详情 可替换[[BusinessManager sharedManager].activityEntityManager requestActivityDetailWithActivityID
 *
 *  @param activityId activityId
 */
- (void)requestActivityDetailWithId:(NSString *)activityId finished:(SEGNetworkRequestCallBack)finished
{
    NSDictionary* paramsDict = [NSDictionary dictionaryWithObjectsAndKeys:activityId,@"serviceId",nil];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/detail.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:paramsDict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSDictionary *responseDic = result;
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        //        NSDictionary *data = [responseDic objectForKey:@"data"];
        //数据返回正常
        if ([code isEqualToString:@"0"]) {
            finished(result,nil);
        }else {
            finished(result,nil);
        }
    }];
}

/**
 *  获取参加活动的成员列表 可替换[[BusinessManager sharedManager].activityMemberEntityManager requestActivityMemberEntityListWithActivityID:
 *
 *  @param activityId activityId
 */
- (void)requestActivityMemberListWithId:(NSString *)activityId finished:(SEGNetworkRequestCallBack)finished
{
    NSDictionary* paramsDict = [NSDictionary dictionaryWithObjectsAndKeys:activityId,@"serviceId",nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/queryActivityUser.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:paramsDict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        NSDictionary *responseDic = result;
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        NSDictionary *data = [responseDic objectForKey:@"data"];
        //数据返回正常
        if ([code isEqualToString:@"0"]) {
            NSArray *memberArray = [data objectForKey:@"result"];
            if (memberArray && memberArray.count > 0) {
                NSMutableArray *modelArray = [NSMutableArray array];
                for (NSDictionary *member in memberArray) {
                    ActivityMemberModel *model = [ActivityMemberModel initWithDictionary:member];
                    [modelArray addObject:model];
                }
                finished(modelArray,nil);
            }
            
        }
    }];
}

/**
 *  参加活动时验证活动人数 可替换[[BusinessManager sharedManager].activityEntityManager requestCheckActivityPersonNum:
 *
 *  @param activityId activityId
 */
- (void)requestCheckActivityPersonNumWithId:(NSString *)activityId finished:(SEGNetworkRequestCallBack)finished
{
    NSDictionary* paramsDict = [NSDictionary dictionaryWithObjectsAndKeys:activityId,@"serviceId",nil];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/checkActivityPersonNum.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:paramsDict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSDictionary *responseDic = result;
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        finished(responseDic,nil);
    }];
}

/**
 *  参加活动时验证活动人数 可替换[[BusinessManager sharedManager].activityEntityManager requestJionActivityWithID:
 *
 *  @param activityId activityId
 */
- (void)requestJoinActivityWithId:(NSString *)activityId andPersonNum:(NSString *)personNum finished:(SEGNetworkRequestCallBack)finished
{
    NSDictionary *paramsDict = [NSDictionary dictionaryWithObjectsAndKeys:activityId,@"serviceId",personNum,@"personNum",nil];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/activity/save.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:paramsDict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSDictionary *responseDic = result;
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        finished(responseDic,nil);
    }];
}

/**
 *  参加活动时验证活动人数 可替换[[BusinessManager sharedManager].activityEntityManager requestStartActivityWithActivityID:
 *
 *  @param activityId activityId
 */
- (void)requestActivityStartOrStopWithId:(NSString *)activityId andGroupChatID:(NSString *)groupChatID finished:(SEGNetworkRequestCallBack)finished
{
    NSDictionary *paramsDict = [NSDictionary dictionaryWithObjectsAndKeys:activityId,@"serviceId",[NSNumber numberWithInt:2],@"status",groupChatID,@"activityGroup",nil];
    
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:kActivityStartOrStopUrl parameters:paramsDict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        NSDictionary *responseDic = result;
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        finished(responseDic,nil);
    }];
}


/**
 *  删除活动
 *
 *  @param activityId activityId
 */
- (void)requestDeleteActivityWithId:(NSString *)activityId finished:(SEGNetworkReponse)finished
{
    NSDictionary *paramsDict = [NSDictionary dictionaryWithObjectsAndKeys:activityId,@"serviceId",[NSNumber numberWithInt:1],@"status",nil];
    
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:kActivityStartOrStopUrl parameters:paramsDict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        NSDictionary *responseDic = result;
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        finished(isSuccessed, result, codeType, errorMessage);
    }];
}

@end
